{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e8761049",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/llama_dataset/labelled-rag-datasets.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a481b689-d363-40a9-a8a4-7d8c6a3a67bd",
   "metadata": {},
   "source": [
    "# Benchmarking RAG Pipelines With A `LabelledRagDatatset`\n",
    "\n",
    "The `LabelledRagDataset` is meant to be used for evaluating any given RAG pipeline, for which there could be several configurations (i.e. choosing the `LLM`, values for the `similarity_top_k`, `chunk_size`, and others). We've likened this abstract to traditional machine learning datastets, where `X` features are meant to predict a ground-truth label `y`. In this case, we use the `query` as well as the retrieved `contexts` as the \"features\" and the answer to the query, called `reference_answer` as the ground-truth label.\n",
    "\n",
    "And of course, such datasets are comprised of observations or examples. In the case of `LabelledRagDataset`, these are made up with a set of `LabelledRagDataExample`'s.\n",
    "\n",
    "In this notebook, we will show how one can construct a `LabelledRagDataset` from scratch. Please note that the alternative to this would be to simply download a community supplied `LabelledRagDataset` from `llama-hub` in order to evaluate/benchmark your own RAG pipeline on it."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9d14822-2779-4da9-bfb7-201f361b8eb1",
   "metadata": {},
   "source": [
    "### The `LabelledRagDataExample` Class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0d66982",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-llms-openai\n",
    "%pip install llama-index-readers-wikipedia"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5017a52c-e61b-4172-b996-c7d7ce56c825",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.llama_dataset import (\n",
    "    LabelledRagDataExample,\n",
    "    CreatedByType,\n",
    "    CreatedBy,\n",
    ")\n",
    "\n",
    "# constructing a LabelledRagDataExample\n",
    "query = \"This is a test query, is it not?\"\n",
    "query_by = CreatedBy(type=CreatedByType.AI, model_name=\"gpt-4\")\n",
    "reference_answer = \"Yes it is.\"\n",
    "reference_answer_by = CreatedBy(type=CreatedByType.HUMAN)\n",
    "reference_contexts = [\"This is a sample context\"]\n",
    "\n",
    "rag_example = LabelledRagDataExample(\n",
    "    query=query,\n",
    "    query_by=query_by,\n",
    "    reference_contexts=reference_contexts,\n",
    "    reference_answer=reference_answer,\n",
    "    reference_answer_by=reference_answer_by,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f295953f-bbe1-4cc2-8a60-1eb75678d57e",
   "metadata": {},
   "source": [
    "The `LabelledRagDataExample` is a Pydantic `Model` and so, going from `json` or `dict` (and vice-versa) is possible."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bffa5b37-4453-49c4-8527-9a5c772dd436",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"query\": \"This is a test query, is it not?\", \"query_by\": {\"model_name\": \"gpt-4\", \"type\": \"ai\"}, \"reference_contexts\": [\"This is a sample context\"], \"reference_answer\": \"Yes it is.\", \"reference_answer_by\": {\"model_name\": \"\", \"type\": \"human\"}}\n"
     ]
    }
   ],
   "source": [
    "print(rag_example.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c86da0f2-c11b-41b1-bfe7-8c5be9f51a0d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LabelledRagDataExample(query='This is a test query, is it not?', query_by=CreatedBy(model_name='gpt-4', type=<CreatedByType.AI: 'ai'>), reference_contexts=['This is a sample context'], reference_answer='Yes it is.', reference_answer_by=CreatedBy(model_name='', type=<CreatedByType.HUMAN: 'human'>))"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LabelledRagDataExample.parse_raw(rag_example.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a23231be-6cc1-49f7-81e9-b2721d1ac836",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'query': 'This is a test query, is it not?',\n",
       " 'query_by': {'model_name': 'gpt-4', 'type': <CreatedByType.AI: 'ai'>},\n",
       " 'reference_contexts': ['This is a sample context'],\n",
       " 'reference_answer': 'Yes it is.',\n",
       " 'reference_answer_by': {'model_name': '',\n",
       "  'type': <CreatedByType.HUMAN: 'human'>}}"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rag_example.dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3040e95-d459-4193-8c45-5238001f1da1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LabelledRagDataExample(query='This is a test query, is it not?', query_by=CreatedBy(model_name='gpt-4', type=<CreatedByType.AI: 'ai'>), reference_contexts=['This is a sample context'], reference_answer='Yes it is.', reference_answer_by=CreatedBy(model_name='', type=<CreatedByType.HUMAN: 'human'>))"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LabelledRagDataExample.parse_obj(rag_example.dict())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97213dd9-c828-4781-af09-c690d9234103",
   "metadata": {},
   "source": [
    "Let's create a second example, so we can have a (slightly) more interesting `LabelledRagDataset`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1986f16f-3f80-4c22-89a0-4d9fa1475d87",
   "metadata": {},
   "outputs": [],
   "source": [
    "query = \"This is a test query, is it so?\"\n",
    "reference_answer = \"I think yes, it is.\"\n",
    "reference_contexts = [\"This is a second sample context\"]\n",
    "\n",
    "rag_example_2 = LabelledRagDataExample(\n",
    "    query=query,\n",
    "    query_by=query_by,\n",
    "    reference_contexts=reference_contexts,\n",
    "    reference_answer=reference_answer,\n",
    "    reference_answer_by=reference_answer_by,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "709422c5-8d1d-462a-bfe8-2eabc73c077f",
   "metadata": {},
   "source": [
    "### The `LabelledRagDataset` Class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2750320c-ae60-455b-a79c-e8774bf4fc89",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.llama_dataset import LabelledRagDataset\n",
    "\n",
    "rag_dataset = LabelledRagDataset(examples=[rag_example, rag_example_2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2812220-f176-49bd-af7c-9a01c3a2dd2a",
   "metadata": {},
   "source": [
    "There exists a convienience method to view the dataset as a `pandas.DataFrame`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1511ddc-c0ed-4aeb-9ff8-76b090d54dde",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>query</th>\n",
       "      <th>reference_contexts</th>\n",
       "      <th>reference_answer</th>\n",
       "      <th>reference_answer_by</th>\n",
       "      <th>query_by</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>This is a test query, is it not?</td>\n",
       "      <td>[This is a sample context]</td>\n",
       "      <td>Yes it is.</td>\n",
       "      <td>human</td>\n",
       "      <td>ai (gpt-4)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>This is a test query, is it so?</td>\n",
       "      <td>[This is a second sample context]</td>\n",
       "      <td>I think yes, it is.</td>\n",
       "      <td>human</td>\n",
       "      <td>ai (gpt-4)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              query                 reference_contexts  \\\n",
       "0  This is a test query, is it not?         [This is a sample context]   \n",
       "1   This is a test query, is it so?  [This is a second sample context]   \n",
       "\n",
       "      reference_answer reference_answer_by    query_by  \n",
       "0           Yes it is.               human  ai (gpt-4)  \n",
       "1  I think yes, it is.               human  ai (gpt-4)  "
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rag_dataset.to_pandas()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f541811-043f-4065-9905-0734173f329f",
   "metadata": {},
   "source": [
    "#### Serialization"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b63716f0-7723-4f21-9f80-703b257f8b48",
   "metadata": {},
   "source": [
    "To persist and load the dataset to and from disk, there are the `save_json` and `from_json` methods."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b3a2781-b4c4-49e6-8245-e2381aacc833",
   "metadata": {},
   "outputs": [],
   "source": [
    "rag_dataset.save_json(\"rag_dataset.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6400b2c3-5b49-40c2-897b-ac3819beb87a",
   "metadata": {},
   "outputs": [],
   "source": [
    "reload_rag_dataset = LabelledRagDataset.from_json(\"rag_dataset.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "111e13eb-120a-434c-a04b-5c0b9fdcd60f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>query</th>\n",
       "      <th>reference_contexts</th>\n",
       "      <th>reference_answer</th>\n",
       "      <th>reference_answer_by</th>\n",
       "      <th>query_by</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>This is a test query, is it not?</td>\n",
       "      <td>[This is a sample context]</td>\n",
       "      <td>Yes it is.</td>\n",
       "      <td>human</td>\n",
       "      <td>ai (gpt-4)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>This is a test query, is it so?</td>\n",
       "      <td>[This is a second sample context]</td>\n",
       "      <td>I think yes, it is.</td>\n",
       "      <td>human</td>\n",
       "      <td>ai (gpt-4)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              query                 reference_contexts  \\\n",
       "0  This is a test query, is it not?         [This is a sample context]   \n",
       "1   This is a test query, is it so?  [This is a second sample context]   \n",
       "\n",
       "      reference_answer reference_answer_by    query_by  \n",
       "0           Yes it is.               human  ai (gpt-4)  \n",
       "1  I think yes, it is.               human  ai (gpt-4)  "
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reload_rag_dataset.to_pandas()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "166f98e8-239a-4007-8754-5a9b4edb41a4",
   "metadata": {},
   "source": [
    "### Building a synthetic `LabelledRagDataset` over Wikipedia \n",
    "\n",
    "For this section, we'll first create a `LabelledRagDataset` using a synthetic generator. Ultimately, we will use GPT-4 to produce both the `query` and `reference_answer` for the synthetic `LabelledRagDataExample`'s.\n",
    "\n",
    "NOTE: if one has queries, reference answers, and contexts over a text corpus, then it is not necessary to use data synthesis to be able to predict and subsequently evaluate said predictions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63cc6fc1-376c-44ca-8370-a0849e96265e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import nest_asyncio\n",
    "\n",
    "nest_asyncio.apply()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4cdd40c6-9912-4a2d-bb12-654e1784da7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install wikipedia -q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dbebaa77-45a0-45d3-8fb8-8570d5c6c5b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# wikipedia pages\n",
    "from llama_index.readers.wikipedia import WikipediaReader\n",
    "from llama_index.core import VectorStoreIndex\n",
    "\n",
    "cities = [\n",
    "    \"San Francisco\",\n",
    "]\n",
    "\n",
    "documents = WikipediaReader().load_data(\n",
    "    pages=[f\"History of {x}\" for x in cities]\n",
    ")\n",
    "index = VectorStoreIndex.from_documents(documents)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "160c6567-b420-4d5c-a022-dfb4889da795",
   "metadata": {},
   "source": [
    "The `RagDatasetGenerator` can be built over a set of documents to generate `LabelledRagDataExample`'s."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f930d75-7a93-4d4c-a647-5f8eeefe37ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "832596c5d7914cc5ae8cfafffb652d99",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Parsing nodes:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# generate questions against chunks\n",
    "from llama_index.core.llama_dataset.generator import RagDatasetGenerator\n",
    "from llama_index.llms.openai import OpenAI\n",
    "\n",
    "# set context for llm provider\n",
    "llm = OpenAI(model=\"gpt-3.5-turbo\", temperature=0.3)\n",
    "\n",
    "# instantiate a DatasetGenerator\n",
    "dataset_generator = RagDatasetGenerator.from_documents(\n",
    "    documents,\n",
    "    llm=llm,\n",
    "    num_questions_per_chunk=2,  # set the number of questions per nodes\n",
    "    show_progress=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf592778-a914-4bbf-a88b-3bbc715d70d5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(dataset_generator.nodes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "400c3dc4-3109-411c-87eb-2d130ff757a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|███████████████████████████████████████████████████████| 13/13 [00:02<00:00,  5.04it/s]\n",
      "100%|█████████████████████████████████████████████████████████| 2/2 [00:02<00:00,  1.14s/it]\n",
      "100%|█████████████████████████████████████████████████████████| 2/2 [00:05<00:00,  2.95s/it]\n",
      "100%|█████████████████████████████████████████████████████████| 2/2 [00:13<00:00,  6.55s/it]\n",
      "100%|█████████████████████████████████████████████████████████| 2/2 [00:07<00:00,  3.89s/it]\n",
      "100%|█████████████████████████████████████████████████████████| 2/2 [00:05<00:00,  2.66s/it]\n",
      "100%|█████████████████████████████████████████████████████████| 2/2 [00:05<00:00,  2.85s/it]\n",
      "100%|█████████████████████████████████████████████████████████| 2/2 [00:04<00:00,  2.03s/it]\n",
      "100%|█████████████████████████████████████████████████████████| 2/2 [00:08<00:00,  4.07s/it]\n",
      "100%|█████████████████████████████████████████████████████████| 2/2 [00:06<00:00,  3.48s/it]\n",
      "100%|█████████████████████████████████████████████████████████| 2/2 [00:04<00:00,  2.34s/it]\n",
      "100%|█████████████████████████████████████████████████████████| 2/2 [00:02<00:00,  1.50s/it]\n",
      "100%|█████████████████████████████████████████████████████████| 2/2 [00:08<00:00,  4.35s/it]\n",
      "100%|█████████████████████████████████████████████████████████| 2/2 [00:08<00:00,  4.34s/it]\n"
     ]
    }
   ],
   "source": [
    "# since there are 13 nodes, there should be a total of 26 questions\n",
    "rag_dataset = dataset_generator.generate_dataset_from_nodes()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2052a43e-3a7a-4f0f-9f0c-7542ffbb64d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>query</th>\n",
       "      <th>reference_contexts</th>\n",
       "      <th>reference_answer</th>\n",
       "      <th>reference_answer_by</th>\n",
       "      <th>query_by</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>How did the gold rush of 1849 impact the devel...</td>\n",
       "      <td>[The history of the city of San Francisco, Cal...</td>\n",
       "      <td>The gold rush of 1849 had a significant impact...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>What were the early European settlements estab...</td>\n",
       "      <td>[The history of the city of San Francisco, Cal...</td>\n",
       "      <td>The early European settlements established in ...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>How did the arrival of Europeans impact the se...</td>\n",
       "      <td>[== Arrival of Europeans and early settlement ...</td>\n",
       "      <td>The arrival of Europeans had a significant imp...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>What were some of the challenges faced by the ...</td>\n",
       "      <td>[== Arrival of Europeans and early settlement ...</td>\n",
       "      <td>The early settlers of San Francisco faced seve...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>How did the California gold rush impact the po...</td>\n",
       "      <td>[== 1848 gold rush ==\\nThe California gold rus...</td>\n",
       "      <td>The California gold rush in the mid-19th centu...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Discuss the role of Chinese immigrants in the ...</td>\n",
       "      <td>[== 1848 gold rush ==\\nThe California gold rus...</td>\n",
       "      <td>Chinese immigrants played a significant role i...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>How did San Francisco transform into a major c...</td>\n",
       "      <td>[== Paris of the West ==\\n\\nIt was during the ...</td>\n",
       "      <td>San Francisco transformed into a major city du...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>What were some significant developments and ch...</td>\n",
       "      <td>[== Paris of the West ==\\n\\nIt was during the ...</td>\n",
       "      <td>During the late 19th and early 20th centuries,...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>How did Abe Ruef contribute to Eugene Schmitz'...</td>\n",
       "      <td>[== Corruption and graft trials ==\\n\\nMayor Eu...</td>\n",
       "      <td>Abe Ruef contributed $16,000 to Eugene Schmitz...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Describe the impact of the 1906 earthquake and...</td>\n",
       "      <td>[== Corruption and graft trials ==\\n\\nMayor Eu...</td>\n",
       "      <td>The 1906 earthquake and fire had a devastating...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>How did the 1906 San Francisco earthquake impa...</td>\n",
       "      <td>[=== Reconstruction ===\\nAlmost immediately af...</td>\n",
       "      <td>The 1906 San Francisco earthquake had a signif...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>What major events and developments took place ...</td>\n",
       "      <td>[=== Reconstruction ===\\nAlmost immediately af...</td>\n",
       "      <td>During the 1930s and World War II, several maj...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>How did the post-World War II era contribute t...</td>\n",
       "      <td>[== Post-World War II ==\\nAfter World War II, ...</td>\n",
       "      <td>After World War II, many American military per...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>Discuss the impact of urban renewal initiative...</td>\n",
       "      <td>[== Post-World War II ==\\nAfter World War II, ...</td>\n",
       "      <td>M. Justin Herman led urban renewal initiatives...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>How did San Francisco become a center of count...</td>\n",
       "      <td>[== 1960 – 1970s ==\\n\\n\\n=== \"Summer of Love\" ...</td>\n",
       "      <td>San Francisco became a center of countercultur...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>Explain the role of San Francisco as a \"Gay Me...</td>\n",
       "      <td>[== 1960 – 1970s ==\\n\\n\\n=== \"Summer of Love\" ...</td>\n",
       "      <td>During the 1960s and beyond, San Francisco bec...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>How did the construction of BART and Muni impa...</td>\n",
       "      <td>[=== New public infrastructure ===\\nThe 1970s ...</td>\n",
       "      <td>The construction of BART and Muni in the 1970s...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>What were the major challenges faced by San Fr...</td>\n",
       "      <td>[=== New public infrastructure ===\\nThe 1970s ...</td>\n",
       "      <td>In the 1980s, San Francisco faced several majo...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>How did the 1989 Loma Prieta earthquake impact...</td>\n",
       "      <td>[=== 1989 Loma Prieta earthquake ===\\n\\nOn Oct...</td>\n",
       "      <td>The 1989 Loma Prieta earthquake had significan...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>Discuss the effects of the dot-com boom in the...</td>\n",
       "      <td>[=== 1989 Loma Prieta earthquake ===\\n\\nOn Oct...</td>\n",
       "      <td>The dot-com boom in the late 1990s had signifi...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>How did the redevelopment of the Mission Bay n...</td>\n",
       "      <td>[== 2010s ==\\nThe early 2000s and into the 201...</td>\n",
       "      <td>The redevelopment of the Mission Bay neighborh...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>What significant events occurred in San Franci...</td>\n",
       "      <td>[== 2010s ==\\nThe early 2000s and into the 201...</td>\n",
       "      <td>In 2010, the San Francisco Giants won their fi...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>In the context of San Francisco's history, dis...</td>\n",
       "      <td>[=== Cultural themes ===\\nBerglund, Barbara (2...</td>\n",
       "      <td>The 1906 earthquake had a significant impact o...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>How did different ethnic and religious communi...</td>\n",
       "      <td>[=== Cultural themes ===\\nBerglund, Barbara (2...</td>\n",
       "      <td>Two specific communities mentioned in the sour...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>In the context of San Francisco's history, wha...</td>\n",
       "      <td>[=== Gold rush &amp; early days ===\\nHittell, John...</td>\n",
       "      <td>Some significant events and developments durin...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>How did politics shape the growth and transfor...</td>\n",
       "      <td>[=== Gold rush &amp; early days ===\\nHittell, John...</td>\n",
       "      <td>The provided sources offer a comprehensive und...</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "      <td>ai (gpt-3.5-turbo)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                query  \\\n",
       "0   How did the gold rush of 1849 impact the devel...   \n",
       "1   What were the early European settlements estab...   \n",
       "2   How did the arrival of Europeans impact the se...   \n",
       "3   What were some of the challenges faced by the ...   \n",
       "4   How did the California gold rush impact the po...   \n",
       "5   Discuss the role of Chinese immigrants in the ...   \n",
       "6   How did San Francisco transform into a major c...   \n",
       "7   What were some significant developments and ch...   \n",
       "8   How did Abe Ruef contribute to Eugene Schmitz'...   \n",
       "9   Describe the impact of the 1906 earthquake and...   \n",
       "10  How did the 1906 San Francisco earthquake impa...   \n",
       "11  What major events and developments took place ...   \n",
       "12  How did the post-World War II era contribute t...   \n",
       "13  Discuss the impact of urban renewal initiative...   \n",
       "14  How did San Francisco become a center of count...   \n",
       "15  Explain the role of San Francisco as a \"Gay Me...   \n",
       "16  How did the construction of BART and Muni impa...   \n",
       "17  What were the major challenges faced by San Fr...   \n",
       "18  How did the 1989 Loma Prieta earthquake impact...   \n",
       "19  Discuss the effects of the dot-com boom in the...   \n",
       "20  How did the redevelopment of the Mission Bay n...   \n",
       "21  What significant events occurred in San Franci...   \n",
       "22  In the context of San Francisco's history, dis...   \n",
       "23  How did different ethnic and religious communi...   \n",
       "24  In the context of San Francisco's history, wha...   \n",
       "25  How did politics shape the growth and transfor...   \n",
       "\n",
       "                                   reference_contexts  \\\n",
       "0   [The history of the city of San Francisco, Cal...   \n",
       "1   [The history of the city of San Francisco, Cal...   \n",
       "2   [== Arrival of Europeans and early settlement ...   \n",
       "3   [== Arrival of Europeans and early settlement ...   \n",
       "4   [== 1848 gold rush ==\\nThe California gold rus...   \n",
       "5   [== 1848 gold rush ==\\nThe California gold rus...   \n",
       "6   [== Paris of the West ==\\n\\nIt was during the ...   \n",
       "7   [== Paris of the West ==\\n\\nIt was during the ...   \n",
       "8   [== Corruption and graft trials ==\\n\\nMayor Eu...   \n",
       "9   [== Corruption and graft trials ==\\n\\nMayor Eu...   \n",
       "10  [=== Reconstruction ===\\nAlmost immediately af...   \n",
       "11  [=== Reconstruction ===\\nAlmost immediately af...   \n",
       "12  [== Post-World War II ==\\nAfter World War II, ...   \n",
       "13  [== Post-World War II ==\\nAfter World War II, ...   \n",
       "14  [== 1960 – 1970s ==\\n\\n\\n=== \"Summer of Love\" ...   \n",
       "15  [== 1960 – 1970s ==\\n\\n\\n=== \"Summer of Love\" ...   \n",
       "16  [=== New public infrastructure ===\\nThe 1970s ...   \n",
       "17  [=== New public infrastructure ===\\nThe 1970s ...   \n",
       "18  [=== 1989 Loma Prieta earthquake ===\\n\\nOn Oct...   \n",
       "19  [=== 1989 Loma Prieta earthquake ===\\n\\nOn Oct...   \n",
       "20  [== 2010s ==\\nThe early 2000s and into the 201...   \n",
       "21  [== 2010s ==\\nThe early 2000s and into the 201...   \n",
       "22  [=== Cultural themes ===\\nBerglund, Barbara (2...   \n",
       "23  [=== Cultural themes ===\\nBerglund, Barbara (2...   \n",
       "24  [=== Gold rush & early days ===\\nHittell, John...   \n",
       "25  [=== Gold rush & early days ===\\nHittell, John...   \n",
       "\n",
       "                                     reference_answer reference_answer_by  \\\n",
       "0   The gold rush of 1849 had a significant impact...  ai (gpt-3.5-turbo)   \n",
       "1   The early European settlements established in ...  ai (gpt-3.5-turbo)   \n",
       "2   The arrival of Europeans had a significant imp...  ai (gpt-3.5-turbo)   \n",
       "3   The early settlers of San Francisco faced seve...  ai (gpt-3.5-turbo)   \n",
       "4   The California gold rush in the mid-19th centu...  ai (gpt-3.5-turbo)   \n",
       "5   Chinese immigrants played a significant role i...  ai (gpt-3.5-turbo)   \n",
       "6   San Francisco transformed into a major city du...  ai (gpt-3.5-turbo)   \n",
       "7   During the late 19th and early 20th centuries,...  ai (gpt-3.5-turbo)   \n",
       "8   Abe Ruef contributed $16,000 to Eugene Schmitz...  ai (gpt-3.5-turbo)   \n",
       "9   The 1906 earthquake and fire had a devastating...  ai (gpt-3.5-turbo)   \n",
       "10  The 1906 San Francisco earthquake had a signif...  ai (gpt-3.5-turbo)   \n",
       "11  During the 1930s and World War II, several maj...  ai (gpt-3.5-turbo)   \n",
       "12  After World War II, many American military per...  ai (gpt-3.5-turbo)   \n",
       "13  M. Justin Herman led urban renewal initiatives...  ai (gpt-3.5-turbo)   \n",
       "14  San Francisco became a center of countercultur...  ai (gpt-3.5-turbo)   \n",
       "15  During the 1960s and beyond, San Francisco bec...  ai (gpt-3.5-turbo)   \n",
       "16  The construction of BART and Muni in the 1970s...  ai (gpt-3.5-turbo)   \n",
       "17  In the 1980s, San Francisco faced several majo...  ai (gpt-3.5-turbo)   \n",
       "18  The 1989 Loma Prieta earthquake had significan...  ai (gpt-3.5-turbo)   \n",
       "19  The dot-com boom in the late 1990s had signifi...  ai (gpt-3.5-turbo)   \n",
       "20  The redevelopment of the Mission Bay neighborh...  ai (gpt-3.5-turbo)   \n",
       "21  In 2010, the San Francisco Giants won their fi...  ai (gpt-3.5-turbo)   \n",
       "22  The 1906 earthquake had a significant impact o...  ai (gpt-3.5-turbo)   \n",
       "23  Two specific communities mentioned in the sour...  ai (gpt-3.5-turbo)   \n",
       "24  Some significant events and developments durin...  ai (gpt-3.5-turbo)   \n",
       "25  The provided sources offer a comprehensive und...  ai (gpt-3.5-turbo)   \n",
       "\n",
       "              query_by  \n",
       "0   ai (gpt-3.5-turbo)  \n",
       "1   ai (gpt-3.5-turbo)  \n",
       "2   ai (gpt-3.5-turbo)  \n",
       "3   ai (gpt-3.5-turbo)  \n",
       "4   ai (gpt-3.5-turbo)  \n",
       "5   ai (gpt-3.5-turbo)  \n",
       "6   ai (gpt-3.5-turbo)  \n",
       "7   ai (gpt-3.5-turbo)  \n",
       "8   ai (gpt-3.5-turbo)  \n",
       "9   ai (gpt-3.5-turbo)  \n",
       "10  ai (gpt-3.5-turbo)  \n",
       "11  ai (gpt-3.5-turbo)  \n",
       "12  ai (gpt-3.5-turbo)  \n",
       "13  ai (gpt-3.5-turbo)  \n",
       "14  ai (gpt-3.5-turbo)  \n",
       "15  ai (gpt-3.5-turbo)  \n",
       "16  ai (gpt-3.5-turbo)  \n",
       "17  ai (gpt-3.5-turbo)  \n",
       "18  ai (gpt-3.5-turbo)  \n",
       "19  ai (gpt-3.5-turbo)  \n",
       "20  ai (gpt-3.5-turbo)  \n",
       "21  ai (gpt-3.5-turbo)  \n",
       "22  ai (gpt-3.5-turbo)  \n",
       "23  ai (gpt-3.5-turbo)  \n",
       "24  ai (gpt-3.5-turbo)  \n",
       "25  ai (gpt-3.5-turbo)  "
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rag_dataset.to_pandas()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f86d33ec-61b7-49a9-ab29-050a82088e74",
   "metadata": {},
   "outputs": [],
   "source": [
    "rag_dataset.save_json(\"rag_dataset.json\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llama_index_3.10",
   "language": "python",
   "name": "llama_index_3.10"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
